/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.product.service.impl.AddPurchaseSettingServiceImpl
   Module Description   :

   Date Created      : 2010/4/28
   Original Author   : afra.chou
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.product.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.service.impl.DomainServiceImpl;
import com.yaodian100.ec.product.entity.AddPurchaseSetting;
import com.yaodian100.ec.product.service.AddPurchaseSettingService;

/**
 * @author afra.chou
 * 
 */
public class AddPurchaseSettingServiceImpl extends DomainServiceImpl<AddPurchaseSetting> implements
		AddPurchaseSettingService {

	@Override
	public List<Object[]> checkPeriod(Date startDate, Date endDate, Long settingId) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT SETTING_ID ");
		sb.append("FROM EC_ADD_PURCHASE_SETTING WHERE");
		sb.append("( START_DATE BETWEEN ? AND ? OR ");
		sb.append(" END_DATE BETWEEN ? AND ?  OR ");
		sb.append(" ? BETWEEN START_DATE AND END_DATE OR ");
		sb.append(" ? BETWEEN START_DATE AND END_DATE  ) ");
		if (settingId != null)
			sb.append("AND SETTING_ID <> ? ");
		List attrs = new ArrayList();
		attrs.add(startDate);
		attrs.add(endDate);
		attrs.add(startDate);
		attrs.add(endDate);
		attrs.add(startDate);
		attrs.add(endDate);
		if (settingId != null)
			attrs.add(settingId);
		return getDao().getSQLQueryByList(sb.toString(), attrs, 0, -1);
	}

	public AddPurchaseSetting getCurrentAddPurchaseSetting() throws Exception {
		CommonCriteria criteria = new CommonCriteria();
		Date today = new Date();
		criteria.addLe("startDate", today);
		criteria.addGe("endDate", today);
		List<AddPurchaseSetting> purchaseSettings = getDao().getList(criteria, null);
		if (purchaseSettings.isEmpty())
			return null;
		else
			return purchaseSettings.get(0);
	}

}
